"""
dict的常用方法（dict由c语言实现）
"""

import copy


def use_dict():
    user_dict = {"Kevin": {"company": "Google"}, "Bob": {"company": "Google"}}

    # 浅拷贝
    new_user_dict = user_dict.copy()
    new_user_dict["Kevin"]["company"] = "Netflix"
    print(user_dict)
    print(new_user_dict)
    # 输出结果：
    """
    {'Kevin': {'company': 'Netflix'}, 'Bob': {'company': 'Google'}}
    {'Kevin': {'company': 'Netflix'}, 'Bob': {'company': 'Google'}}
    """

    # 深拷贝
    user_dict = {"Kevin": {"company": "Google"}, "Bob": {"company": "Google"}}
    new_user_dict = copy.deepcopy(user_dict)
    new_user_dict["Kevin"]["company"] = "Netflix"
    print(user_dict)
    print(new_user_dict)
    # 输出结果：
    """
    {'Kevin': {'company': 'Google'}, 'Bob': {'company': 'Google'}}
    {'Kevin': {'company': 'Netflix'}, 'Bob': {'company': 'Google'}}
    """

    # 获取值
    user = user_dict.get("Kevin", None)
    user_not_exist = user_dict.get("KK", None)
    print(user)
    print(user_not_exist)
    # 输出结果：
    """
    {'company': 'Google'}
    None
    """

    # 设置默认值
    user_dict = {"Kevin": {"company": "Google"}, "Bob": {"company": "Google"}}
    user_dict.setdefault("Tom", "Tesla")
    print(user_dict)
    # 输出结果：
    """
    {'Kevin': {'company': 'Google'}, 'Tom': 'Tesla', 'Bob': {'company': 'Google'}}
    """

    # 更新
    user_dict.update((("Tom", "Alibaba"),))
    print(user_dict)
    # 输出结果：
    """
    {'Tom': 'Alibaba', 'Kevin': {'company': 'Google'}, 'Bob': {'company': 'Google'}}
    """


if __name__ == '__main__':
    use_dict()
